//
//  ST_SurveyViewController_AIVersion.swift
//  btvideo
//
//  Created by mac on 2020/4/8.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit


/**
 新版调研
 */
class ST_SurveyViewController_AIVersion: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.height.equalTo(362)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
//            .offset(-ScreenHeight/10)
        }
        
        
        
        
        root_view.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(100)
            make.top.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
        }
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cover_image_view.snp.bottom).offset(15)
            make.centerX.equalToSuperview()
        }
        
        root_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
        }
        
      
        root_view.addSubview(text_field_root_view)
        text_field_root_view.snp.makeConstraints { (make) in
            make.width.equalTo(260)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(content_label.snp.bottom).offset(25)
        }
        
        text_field_root_view.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 10))
        }
        
        
        root_view.addSubview(cancel_btn)
        root_view.addSubview(confirm_btn)
        
        confirm_btn.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-25)
            make.width.equalTo(160)
            make.height.equalTo(35)
            make.centerX.equalToSuperview()
        }
        
        confirm_btn.alpha = confirmBtnAlpha
        
        cancel_btn.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(26)
            make.height.equalTo(26)
        }
        
        
        titleLabel.text = model.question_title
        content_label.text = model.question_content
    }
    
//    MARK:- Properties
   var model = ST_SurveyModel()
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.white
        control.cornerRadius = 20
        control.borderColor = Color.init(hexString: "#979797")
        control.borderWidth = 1
        return control
    }()
    
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("矩形what")
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
        control.textAlignment = .center
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
        control.textAlignment = .center
        control.textColor = UIColor.init(hex: 0x61666F)
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var textField:UITextField = {
        let control = UITextField()
        
        control.textColor = UIColor.init(hex: 0x333333)
        
        let attrString = NSMutableAttributedString(string: "请输入内容")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        control.attributedPlaceholder = attrString
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        
        control.delegate = self
        return control
    }()
    fileprivate lazy var text_field_root_view:UIView = {
        
        let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xF2F4F7)
        control.cornerRadius = 5
        return control
        
    }()
    
    

    fileprivate var confirmBtnAlpha = 0.6.cgFloat
    
    fileprivate lazy var confirm_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.cornerRadius = 35/2.0
        control.setTitleForAllStates("完成")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var cancel_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
//        control.cornerRadius = 36/2.0
        control.setImageForAllStates(ST_ImageUtils.imageName("特质_icon_关闭")!)
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate var maxCount = 10
    
//    MARK: - Public
    
    @objc var confirm_closure:(()->())?
    @objc var cancel_closure:(()->())?
    
}


extension ST_SurveyViewController_AIVersion:UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        guard var animal = textField.text else{
            return true
        }
        
        if let idxRange = Range.init(range, in: animal){
            
            animal.replaceSubrange(idxRange, with: string)
         
            confirm_btn.alpha = animal.count > 0 ? 1 : confirmBtnAlpha
            
            return animal.count <= maxCount
            
        }else{
            
            return true
        }
    }
    
    @objc func confirm_handle(){
        
        if self.confirm_btn.alpha < 0.9{
            return
        }
        
        guard let text = self.textField.text else{
            
            ToastHud(text: "输入内容不能为空", view: self.view)
            
            return
        }
        
        let param = ["statistics_id":model.statistics_id,"answer":text] as [String : Any]
        
        JVrequest(PH.answerSurveyApi, parameters: param,success: { (json) in
            
            self.dismiss(animated: false) {
                
            }
            
        }) { (code, err) in
            
            ToastHud(text: err, view: self.view)
        }
        
    }
    
    @objc func cancel_handle(){
        
        self.dismiss(animated: false) {
            self.cancel_closure?()
        }
    }
    
    
}

